package demo8;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan("demo8")
@PropertySource("classpath:config.properties")
public class Config {
    // Person相关配置
    @Value("${person.name:DefaultName}")
    private String defaultName;

    @Value("${person.age:18}")
    private int defaultAge;

    // 数据库连接配置
    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name:com.mysql.cj.jdbc.Driver}")
    private String driverClassName;

    @Bean
    public Person initPerson() {
        // 使用注入的属性值创建Person
        return new Person(defaultName, defaultAge);
    }

    @Bean
    public DruidDataSource initDruidDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(dbUrl);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }
    @Bean
    public DBUtil initDBUtil() {
        return new DBUtil(initDruidDataSource());
    }
}
